package com.joonhat.MonkeyFlash;

import java.util.Vector;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.util.Persistable;


interface Observer{
	public abstract void update(Observable observable);
}

class Observable{
	private Vector observers = new Vector(1);
	
	public void addObserver(Observer observer) {
		observers.addElement(observer);
	}
	
	public void notifyObservers(){
		for (int i = 0; i < observers.size(); i++){
			((Observer)observers.elementAt(i)).update(this);
		}
	}
}

public class Option {

	public static final int SIMPLE_MODE = 0;
	public static final int DANCE_MODE = 1;
	
	private static Option option = new Option();
	private static HotkeyOption hotkeyOpition = new HotkeyOption();
	private Database data = Database.getPersistentDatabase();
	private int nextMode;
	
	private Option() {}
	
	public static Option getIns(){
		return option;
	}
	
	public int getNextMode() {
		return nextMode;
	}

	public void setNextMode(int nextMode) {
		this.nextMode = nextMode;
	}
	
	public int getLastMode() {
		return data.getLastMode();
	}
	
	public void setLastMode(int mode) {
		data.setLastMode(mode);
	}
	
	public String getBlinkOnTime() {
		return data.getBlinkOnTime();
	}
	public void setBlinkOnTime(String blinkOnTime) {
		data.setBlinkOnTime(blinkOnTime);
	}

	public String getBlinkOffTime() {
		return data.getBlinkOffTime();
	}
	public void setBlinkOffTime(String blinkOffTime) {
		data.setBlinkOffTime(blinkOffTime);
	}

	public char getHotkey() {
		return hotkeyOpition.getHotkey();
	}
	public void setHotkey(char hotkey) {
		hotkeyOpition.setHotkey(hotkey);
	}
}

class HotkeyOption {
	
	private Database data = Database.getPersistentDatabase();
	
	public char getHotkey() {
		return data.getHotkey();
	}
	public void setHotkey(char hotkey) {
		data.setHotkey(hotkey);
	}
}


class SimpleOption extends Observable{
	
	private static SimpleOption simpleOption = new SimpleOption();
	private Database data = Database.getPersistentDatabase();

	private boolean currentFlashOn;
	private boolean toggleFlash;

	private BrightOption brightScreen = BrightOption.getIns();
	
	private SimpleOption() {
		updateCurrentFlashFromStartupFlash(); 
	}

	public static SimpleOption getIns(){ 
		return simpleOption; 
	}
	
	public void setDuration(String duration){
		data.setDuration(duration);
		notifyObservers();
	}
	public String getDuration(){
		return data.getDuration();
	}
	
	public boolean isLed() {
		return data.isLed();
	}
	public void setLed(boolean isLed) {
		notifyObservers();
		data.setLed(isLed);
	}
	
	public boolean isStartupFlashOn() {
		return data.isStarupFlashOn();
	}
	public void setStartupFlashOn(boolean isFlashOnStarup) {
		data.setStarupFlashOn(isFlashOnStarup);
	}
	
	public boolean isCurrentFlashOn() {
		return currentFlashOn;
	}
	public void setCurrentFlashOn(boolean currentFlashOn) {
		this.currentFlashOn = currentFlashOn;
		notifyObservers();
	}
	
	public boolean isBrightScreen() {
		return brightScreen.isBrightScreen();
	}
	public void setBrightScreen(boolean isBrightScreen) {
		brightScreen.setBrightScreen(isBrightScreen);
	}
	
	public boolean isToggleFlash() {
		return toggleFlash;
	}

	public void setToggleFlash(boolean toggleFlash) {
		this.toggleFlash = toggleFlash;
	}
	
	public void updateCurrentFlashFromStartupFlash(){
		setCurrentFlashOn(data.isStarupFlashOn());
	}
}

class BrightOption extends Observable {
	
	private static BrightOption brightScreen = new BrightOption();
	
	private Database data = Database.getPersistentDatabase();

	private BrightOption() {}

	public static BrightOption getIns(){
		return brightScreen;
	}
	
	public boolean isBrightScreen() {
		return data.isBrightScreen();
	}

	public void setBrightScreen(boolean isBrightScreen) {
		data.setBrightScreen(isBrightScreen);
		notifyObservers();
	}
}

class Database implements Persistable {

	private static Database data;
	private static PersistentObject perObj;
	
	private char hotkey = 'p';
	private int lastMode = 0;
	private String blinkOnTime = "15";
	private String blinkOffTime = "30";
	private String duration = "120000";
	private boolean isBrightScreen = true;
	private boolean isLed = false;
	private boolean isStarupFlashOn = true;
	
	private Database() {}
	
	public static Database getPersistentDatabase(){
		if (data != null) return data;
		loadDatabase();
		return data;
	}

	private static void loadDatabase() {
		perObj = PersistentStore.getPersistentObject(Constant.OPTION_PERSISTENT_KEY);
		if (perObj.getContents() == null){
			perObj.setContents(new Database());
			perObj.commit();
		}
		data = (Database) perObj.getContents();
	}
	
	public char getHotkey() {
		return hotkey;
	}
	public void setHotkey(char hotkey) {
		this.hotkey = hotkey;
	}
	
	public int getLastMode() {
		return lastMode;
	}
	public void setLastMode(int mode) {
		lastMode = mode;
		perObj.commit();
	}
	
	public String getBlinkOnTime() {
		return blinkOnTime;
	}
	public void setBlinkOnTime(String blinkOnTime) {
		this.blinkOnTime = blinkOnTime;
		perObj.commit();
	}

	public String getBlinkOffTime() {
		return blinkOffTime;
	}
	public void setBlinkOffTime(String blinkOffTime) {
		this.blinkOffTime = blinkOffTime;
		perObj.commit();
	}

	public String getDuration() {
		return duration;
	}
	public void setDuration(String duration) {
		this.duration = duration;
		perObj.commit();
	}

	public boolean isBrightScreen() {
		return isBrightScreen;
	}

	public void setBrightScreen(boolean isBrightScreen) {
		this.isBrightScreen = isBrightScreen;
		perObj.commit();
	}

	public boolean isLed() {
		return isLed;
	}
	public void setLed(boolean isLed) {
		this.isLed = isLed;
	}
	
	public boolean isStarupFlashOn() {
		return isStarupFlashOn;
	}
	public void setStarupFlashOn(boolean isStarupFlashOn) {
		this.isStarupFlashOn = isStarupFlashOn;
		perObj.commit();
	}
}
